﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConfeccionesJR.Entites;
using ConfeccionesJR.BussinesLogic;
using Microsoft.Practices.Unity;

namespace ConfeccionesJR.UI
{
    public partial class FrmCompra : Form
    {
        [Dependency]
        public ICompraBL compraBL { get; set; }

        [Dependency]
        public IDetalleCompraBL detallecompraBL { get; set; }

        public FrmCompra()
        {
            InitializeComponent();
            this.BuildUp();
        }

        float f_HeightRatio = new float();
        float f_WidthRatio = new float();

        public void ResizeForm(Form ObjForm, int DesignerHeight, int DesignerWidth)
        {
            #region Code for Resizing and Font Change According to Resolution

            int i_StandardHeight = DesignerHeight;
            int i_StandardWidth = DesignerWidth;
            int i_PresentHeight = Screen.PrimaryScreen.Bounds.Height - 65;
            int i_PresentWidth = Screen.PrimaryScreen.Bounds.Width - 245;
            f_HeightRatio = (float)((float)i_PresentHeight / (float)i_StandardHeight);
            f_WidthRatio = (float)((float)i_PresentWidth / (float)i_StandardWidth);
            //ObjForm.AutoScaleMode = AutoScaleMode.None;
            ObjForm.AutoScaleMode = AutoScaleMode.Dpi;
            ObjForm.Scale(new SizeF(f_WidthRatio, f_HeightRatio));
            foreach (Control c in ObjForm.Controls)
            {
                if (c.HasChildren)
                {
                    ResizeControlStore(c);
                }
                else
                {
                    c.Font = new Font(c.Font.FontFamily, c.Font.Size * f_HeightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
                }
            }
            ObjForm.Font = new Font(ObjForm.Font.FontFamily, ObjForm.Font.Size * f_HeightRatio, ObjForm.Font.Style, ObjForm.Font.Unit, ((byte)(0)));
            #endregion
        }

        private void ResizeControlStore(Control objCtl)
        {
            #region

            if (objCtl.HasChildren)
            {
                foreach (Control cChildren in objCtl.Controls)
                {
                    if (cChildren.HasChildren)
                    {
                        ResizeControlStore(cChildren);
                    }
                    else
                    {
                        cChildren.Font = new Font(cChildren.Font.FontFamily, cChildren.Font.Size * f_HeightRatio, cChildren.Font.Style, cChildren.Font.Unit, ((byte)(0)));
                    }
                }
                objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * f_HeightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
            }
            else
            {
                objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * f_HeightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
            }
            #endregion
        }

        private void FrmCompra_Load(object sender, EventArgs e)
        {
            //this.ResizeForm(this, 700, 1036); 

            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            var fec2 = dateTimePicker1.Value.Date;
            dateTimePicker2.Value = fec2.AddDays(1);

            cOMPRABindingSource.DataSource = compraBL.GetAllFromCompra();
            
            COMPRA currentcompra = cOMPRABindingSource.Current as COMPRA;
            if (currentcompra != null)
            {
                dETALLECOMPRAsBindingSource.DataSource = detallecompraBL.GetAllFromDetalleCompraByCompraId(currentcompra.Id);
                Suma();
            }
            else LTotal.Text = "0.00";

        }

        public void Suma()
        {
            decimal total = 0;
            decimal newtotalDeuda = 0;
            for (int j = 0; j <= dETALLECOMPRADataGridView.RowCount - 1; j++)
            {
                total = total + Convert.ToDecimal(dETALLECOMPRADataGridView[3, j].Value);
                newtotalDeuda = Math.Round(total, 2);
            }
        
            LTotal.Text = Convert.ToString(newtotalDeuda);
        }

        private void cOMPRADataGridView_CurrentCellChanged(object sender, EventArgs e)
        {
            COMPRA currentcompra = cOMPRABindingSource.Current as COMPRA;
            if (currentcompra != null)
            {
                dETALLECOMPRAsBindingSource.DataSource = detallecompraBL.GetAllFromDetalleCompraByCompraId(currentcompra.Id);
                Suma();
            }
            else LTotal.Text = "0.00";
        }

        private void ReiniciarFechas()
        {
            dateTimePicker1.Value = DateTime.Now.Date;
            var fec2 = dateTimePicker1.Value.Date;
            dateTimePicker2.Value = fec2.AddDays(1);
        }

        private  void TodosLosBuscar()
        {
            try
            {
                var fec1 = dateTimePicker1.Value.Date;
                var fec2 = dateTimePicker2.Value.Date;
                
                if (rbtnFechaUnica.Checked == true)
                {
                    lblFec1.Text = "Día:";
                    lblFec2.Visible = false;
                    dateTimePicker1.Enabled = true;
                    dateTimePicker2.Visible = false;
                    if (RBOpProveedor.Checked == true)
                    {
                        cOMPRABindingSource.DataSource = compraBL.GetFromComprabyFechasAndProveedor(fec1, null, TxtDescripcion.Text);
                    }
                    if (RBOpGuia.Checked == true)
                    {

                        cOMPRABindingSource.DataSource = compraBL.GetFromComprabyFechasAndNroGuia(fec1, null, TxtDescripcion.Text);
                    }
                }

                if (rbtnFechas.Checked == true)
                {
                    lblFec1.Text = "Desde:";
                    lblFec1.Visible = true;
                    lblFec2.Visible = true;
                    dateTimePicker1.Enabled = true;
                    dateTimePicker2.Enabled = true;
                    dateTimePicker2.Visible = true;
                    if (fec1 <= fec2)
                    {
                        if (RBOpProveedor.Checked == true)
                        {
                            cOMPRABindingSource.DataSource = compraBL.GetFromComprabyFechasAndProveedor(fec1, fec2, TxtDescripcion.Text);
                        }
                        if (RBOpGuia.Checked == true)
                        {

                            cOMPRABindingSource.DataSource = compraBL.GetFromComprabyFechasAndNroGuia(fec1, fec2, TxtDescripcion.Text);
                        }
                    }
                    else
                    {
                        ReiniciarFechas();
                        MessageBox.Show("La fecha de inicio debe ser menor a la fecha de término", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                if (rbtnSoloCriterios.Checked == true )
                {
                    dateTimePicker1.Visible = true;
                    dateTimePicker2.Visible = true;
                    lblFec1.Visible = true;
                    lblFec2.Visible = true;
                    lblFec1.Text = "Desde:";
                    dateTimePicker1.Enabled = false;
                    dateTimePicker2.Enabled = false;
                    if (RBOpGuia.Checked == true)
                    {
                        cOMPRABindingSource.DataSource = compraBL.GetFromCompraByNroGuia(TxtDescripcion.Text);
                    }
                    if (RBOpProveedor.Checked == true)
                    {
                        cOMPRABindingSource.DataSource = compraBL.GetFromCompraByProveedor(TxtDescripcion.Text);
                    }                    
                }
                var _currentCompra = cOMPRABindingSource.Current as COMPRA;
                {
                    if (_currentCompra != null)
                    {
                        dETALLECOMPRAsBindingSource.DataSource = detallecompraBL.GetFromDetalleCompraByProductoAndCompraId("", _currentCompra.Id);
                    }
                    else
                    {
                        dETALLECOMPRAsBindingSource.Clear();
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("No existen créditos registrados", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        

        private void BtnBuscarProducto_Click(object sender, EventArgs e)
        {
            BuscarEnDetalleCompra();
        }

        private void TxtProductoDesc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BuscarEnDetalleCompra();
            }
        }

        private void BuscarEnDetalleCompra()
        {
            COMPRA currentCompra = cOMPRABindingSource.Current as COMPRA;
            if (currentCompra != null)
            {
                dETALLECOMPRAsBindingSource.DataSource = detallecompraBL.GetFromDetalleCompraByProductoAndCompraId(TxtProductoDesc.Text, currentCompra.Id);
            }
            else
            {
                MessageBox.Show("Debe seleccionar una Compra", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            frmMttoCompra frm = new frmMttoCompra();
            frm.Nuevo();
            frm.ShowDialog();
            cOMPRABindingSource.DataSource = compraBL.GetAllFromCompra();
        }

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            var currentCompra = cOMPRABindingSource.Current as COMPRA;
            if (currentCompra != null)
            {
                frmMttoCompra frm = new frmMttoCompra();
                frm.Editar(currentCompra.Id);
                frm.ShowDialog();
                cOMPRABindingSource.DataSource = compraBL.GetAllFromCompra();
            }
            else
            {
                MessageBox.Show("Debe seleccionar una Compra", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void rbtnFechaUnica_Click(object sender, EventArgs e)
        {
            TodosLosBuscar();
        }

        private void rbtnFechas_Click(object sender, EventArgs e)
        {
            TodosLosBuscar();
        }

        private void rbtnSoloCriterios_Click(object sender, EventArgs e)
        {
            TodosLosBuscar();
        }

        private void RBOpGuia_Click(object sender, EventArgs e)
        {
            TodosLosBuscar();
        }

        private void RBOpProveedor_Click(object sender, EventArgs e)
        {
            TodosLosBuscar();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            TodosLosBuscar();
        }

        private void TxtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TodosLosBuscar();
            }
        }

        private void cOMPRADataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Suma();
        }

     

    }
}
